#!/bin/bash
source .env
source ./scripts/_functions.sh

declare -A paths=( \
  ["themes" ]="wp-content/themes/mysite/" \
  ["plugins"]="wp-content/plugins/" )

function encode_password() {
  perl -e 'use URI::Escape; $encoded = uri_escape("'$1'"); print "$encoded\n";'
}

function upload_files() {
  bn="wordpress"
  password=$(encode_password $FTP_PASSWORD)
  wput -v --basename="$bn/" "$bn/$1" \
    "ftp://${FTP_USER}:${password}@${FTP_HOST}${FTP_REMOTEPATH}/"
}

if [ $1 ]; then
  upload_files "${paths[$1]}"

else
  for name in ${!paths[@]}; do
    upload_files "${paths[$name]}"
  done
fi
